{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: jieba in c:\\users\\administrator\\anaconda3\\lib\\site-packages (0.42.1)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "DEPRECATION: pandas 0.23.4 has a non-standard dependency specifier pytz>=2011k. pip 24.1 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pandas or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\n"
     ]
    }
   ],
   "source": [
    "!pip install jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-3-4e8a61810eb4>, line 19)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-3-4e8a61810eb4>\"\u001b[1;36m, line \u001b[1;32m19\u001b[0m\n\u001b[1;33m    print(\"'刘备'和'曹操'的相似度：{}\".format(model.wv.similarity('刘备'，'曹操')))\u001b[0m\n\u001b[1;37m                                                            ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "import re\n",
    "from gensim.models import Word2Vec\n",
    "with open(r\"data/sanguo.txt\",encoding='utf-8') as f:\n",
    "    lines=[]\n",
    "    for line in f:\n",
    "        temp=jieba.lcut(line)\n",
    "        words=[]\n",
    "        for i in temp:\n",
    "            i = re.sub(\"[\\s+\\.\\!\\/_,$%^*(+\\\"\\'””《》]+|[+——！，。？、~@#￥%……&*（）：；‘]+\", \"\", i)\n",
    "            if len(i)>0:\n",
    "                words.append(i)\n",
    "                if len(words)>0:\n",
    "                    lines.append(words)\n",
    "model=Word2Vec(lines,vector_sizer=20,window=2,min_count=3,epochs=7,negative=10,sg=1)\n",
    "print(\"'周瑜'的词向量为：\\n\",model.wv.get_vector('周瑜'))\n",
    "print(\"与'周瑜'相似度最高的10个词是：\")\n",
    "print(model.wv.most_similar('周瑜',topn=10))\n",
    "print(\"'刘备'和'曹操'的相似度：{}\".format(model.wv.similarity('刘备'，'曹操')))\n",
    "words=\"孙权 曹操 刘备 孙夫人\"\n",
    "print(\"在词'孙权/曹操/刘备/孙夫人'中，'{}'与其他词不属于同一类\".format(model.wv.doesnt_match(words.split())))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gensim\n",
    "from gensim.models.doc2vec import Doc2Eec\n",
    "TaggededDocument=gensim.models.doc2vec.TaggedDocument\n",
    "def get_data():\n",
    "    with open(\"data/train.txt\",'r',encoding='utf-8') as f:\n",
    "        docs=f.readlines()\n",
    "        train_data=[]\n",
    "        for i,text in enumerate(docs):\n",
    "            word_list=text.split('')\n",
    "            word_list[len(word_list)-1]=word_list[len(word_list)-1].strip()\n",
    "            document=TaggededDocument(word_list,tags=[i])\n",
    "            train_data.append(document)\n",
    "            return train_data\n",
    "        def train_model(x_train):\n",
    "            model_dm=Doc2Vec(x_train,min_count=1,,window=3,vector_size=20,negative=5,workers=4,dm=1)\n",
    "            model_dm"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
